package com.bRoom.service.impl;

import com.bRoom.common.Conts;
import com.bRoom.common.ServerResponse;
import com.bRoom.dao.RoomMapper;
import com.bRoom.pojo.PayInfo;
import com.bRoom.pojo.Room;
import com.bRoom.service.IRoomService;
import com.bRoom.util.DateTimeUtil;
import com.bRoom.util.PropertiesUtil;
import com.bRoom.vo.RoomDetailVo;
import com.bRoom.vo.RoomListVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service("iRoomService")
public class RoomServiceImpl implements IRoomService {


    @Autowired
    private RoomMapper roomMapper;



    public  ServerResponse<Room> addRoom(Room room){

         String roomNum=room.getRoomNumber();
         int checkRoomNum=roomMapper.checkRoomNum(roomNum);
         if (checkRoomNum>0){
              return ServerResponse.createByErrorMsg("房间编号已存在");
         }
         int addRoomCount=roomMapper.insert(room);
         if (addRoomCount>0){
              return ServerResponse.createBySuccessMsg("房间添加成功");
         }
         return  ServerResponse.createByErrorMsg("房间添加失败");
    }

    public ServerResponse<Room> updateRoom(Room room){

             Room updateRoom=new Room();
             updateRoom.setRoomId(room.getRoomId());
        updateRoom.setRoomName(room.getRoomName());
        updateRoom.setRoomNumber(room.getRoomNumber());
        updateRoom.setRoomPrice(room.getRoomPrice());
        updateRoom.setRoomMessage(room.getRoomMessage());
        updateRoom.setRoomImg(room.getRoomImg());
        updateRoom.setRoomType(room.getRoomType());
        updateRoom.setRoomStatus(Conts.ROOM_STATUS.ROOM_BE_SELL.getCode());
        updateRoom.setRoomName(room.getRoomName());
        String updateTime=DateTimeUtil.DatetoStr(new Date());
        updateRoom.setUpdatetime(DateTimeUtil.StringToDate(updateTime));

          int updateRoomCount=roomMapper.updateByPrimaryKeySelective(room);

          if (updateRoomCount>0){
               return ServerResponse.createBySuccessMsg("修改房间信息成功");
          }
          return ServerResponse.createByErrorMsg("修改房间信息失败");
    }

    public ServerResponse stopRoom(Integer roomId){
           int roomStatus= Conts.ROOM_STATUS.ROOM_STOP.getCode();
           int updateRoomStatus=roomMapper.updateRoomStatus(roomId,roomStatus);
           if (updateRoomStatus>0){
                return ServerResponse.createBySuccessMsgData("房间停用成功",Conts.ROOM_STATUS.ROOM_STOP.getDesc());
           }
           return ServerResponse.createByErrorMsg("房间停用失败");
    }

    public  ServerResponse checkOutRoom(Integer roomId){
           int roomStatus=Conts.ROOM_STATUS.ROOM_BE_OUT.getCode();
           int checkOutCount=roomMapper.updateRoomStatus(roomId,roomStatus);
           if (checkOutCount>0){
                return ServerResponse.createBySuccessMsgData("正在退房",Conts.ROOM_STATUS.ROOM_BE_OUT.getDesc());
           }
           return ServerResponse.createByErrorMsg("退房失败");
    }

    public ServerResponse<RoomDetailVo> roomDetail(Integer roomId){
           Room room=roomMapper.roomDetail(roomId);
           if (room==null){
                return ServerResponse.createByErrorMsg("信息查找失败");
           }

           RoomDetailVo roomDetailVo=assambleRoomDetailVo(room);
           return ServerResponse.createBySuccessData(roomDetailVo);
    }


    public ServerResponse<PageInfo> roomList(int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Room> roomList=roomMapper.selectRoomList();

       if (roomList==null){
             return ServerResponse.createByErrorMsg("找不到相关信息");
        }
        List<RoomListVo> roomListVoList=Lists.newArrayList();
         for(Room roomItem :roomList){
               RoomListVo roomListVo=assambleRoomListVo(roomItem);
               roomListVoList.add(roomListVo);
         }
          PageInfo resultPage=new PageInfo(roomList);
         resultPage.setList(roomListVoList);

        return ServerResponse.createBySuccessData(resultPage);
    }


    public ServerResponse<PageInfo> searchRoom(String roomName,int pageNum,int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        if (StringUtils.isNotBlank(roomName)){
            roomName=new StringBuilder().append("%").append(roomName).append("%").toString();
            List<Room> roomList=roomMapper.selectEarch(roomName);
            if (CollectionUtils.isEmpty(roomList)){
                 return ServerResponse.createBySuccessMsg("找不到相关的房间");
            }

            List<RoomListVo> roomListVoList=Lists.newArrayList();
            for (Room roomItem:roomList){
                      RoomListVo roomListVo=assambleRoomListVo(roomItem);
                      roomListVoList.add(roomListVo);
            }
            PageInfo resulrPage=new PageInfo(roomList);
            resulrPage.setList(roomListVoList);
            return ServerResponse.createBySuccessData(resulrPage);
        }
   return ServerResponse.createByErrorMsg("参数错误");
    }

    private RoomListVo  assambleRoomListVo(Room room){
                   RoomListVo roomListVo=new RoomListVo();
                   roomListVo.setRoomId(room.getRoomId());
                   roomListVo.setRoomName(room.getRoomName());
                   roomListVo.setRoomNumber(room.getRoomNumber());
                   roomListVo.setRoomPrice(room.getRoomPrice());
                   roomListVo.setRoomStatus(room.getRoomStatus());
                   roomListVo.setRoomType(room.getRoomType());
                   roomListVo.setCreatetime(DateTimeUtil.DatetoStr(room.getCreatetime()                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                ));
                   roomListVo.setUpdatetime(DateTimeUtil.DatetoStr(room.getUpdatetime()));

                  return roomListVo;
    }

    private RoomDetailVo assambleRoomDetailVo(Room room){
            RoomDetailVo roomDetailVo=new RoomDetailVo();
            roomDetailVo.setRoomId(room.getRoomId());
            roomDetailVo.setRoomName(room.getRoomName());
            roomDetailVo.setRoomNumber(room.getRoomNumber());
            roomDetailVo.setRoomPrice(room.getRoomPrice());
            roomDetailVo.setRoomComment(room.getRoomComment());
            roomDetailVo.setRoomMessage(room.getRoomMessage());
            roomDetailVo.setRoomImg(room.getRoomImg());
            roomDetailVo.setRoomStatus(room.getRoomStatus());
            roomDetailVo.setRoomType(room.getRoomType());
            roomDetailVo.setHotelName(room.getHotelName());
            roomDetailVo.setCreatetime(DateTimeUtil.DatetoStr(room.getCreatetime()));
            roomDetailVo.setUpdatetime(DateTimeUtil.DatetoStr(room.getUpdatetime()));
           roomDetailVo.setImgHost(PropertiesUtil.getProperties("ftp.server.http.prefix","http://image.imooc.com:8089/"));

          return roomDetailVo;
    }



}
